package com.hs.learn.service;

import com.hs.learn.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: 测试mongodb声明式事务
 * @Author 胡尚
 * @Date: 2024/7/31 14:58
 */
@Service
public class EmployeeService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Transactional(rollbackFor = Exception.class)
    public void addEmployee(){
        Employee employee1 = new Employee(1, "hushang1", 25, new Date(), "测试数据1");
        Employee employee2 = new Employee(2, "hushang2", 25, new Date(), "测试数据2");

        mongoTemplate.insert(employee1);
//        int i = 1/0;
        mongoTemplate.insert(employee2);
    }
}
